package Netzwerk;

import java.sql.*;
import java.util.*; 
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.applet.*;
import java.security.*;

/*
 * DB Zugriffdaten:
 * 	Datenbank: bomberman
	Benutzer: team04
	Passwort: Bomberman
	Zugang von den IPs: %
	Kollation: utf8_general_ci
	Email: jlessmann@gmx.de
	Host:	db4free.net
 */


	public class DB_Zugriff {
		
		private String ip_adressen[] = new String[100];
	    int i = 0;
		private int anzahl = 0;
		
		
	    public void db_auslesen(String abfrage){
	    HttpURLConnection conn=null;
	    try{
	    URL url=new URL("http://joergy.funpic.de/db_zugriff.php");
	    String agent="Applet";
	    String query="query=" + abfrage;
	    String type="application/x-www-form-urlencoded";
	    conn=(HttpURLConnection)url.openConnection();
	    conn.setDoInput(true);
	    conn.setDoOutput(true);
	    conn.setRequestMethod("POST");
	    conn.setRequestProperty( "User-Agent", agent );
	    conn.setRequestProperty( "Content-Type", type );
	    conn.setRequestProperty( "Content-Length", ""+query.length());

	    OutputStream out=conn.getOutputStream();
	    out.write(query.getBytes());
	    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
	    String inputLine;
	    while((inputLine=in.readLine())!=null){
	        StringTokenizer st = new StringTokenizer(inputLine);
	        while (st.hasMoreTokens()) {
	        	ip_adressen[i] = st.nextToken();
//	        	System.out.print(ip_adressen[i]+"\n");
	        	i++;
	        	anzahl++;
	        }
	    };
	    in.close();
	    int rc = conn.getResponseCode();
	    System.out.print("Response Code = "+rc+"\n");
	    String rm=conn.getResponseMessage();
	    System.out.print("Response Message = "+rm+"\n");
	    }catch(Exception e){
	    e.printStackTrace();
	    }finally{
	    conn.disconnect();
	    }
	    }

	    public void db_einschreiben(String insert){
		    HttpURLConnection conn=null;
		    try{
		    URL url=new URL("http://joergy.funpic.de/db_zugriff.php");
		    String agent="Applet";
		    String query= "query=" + insert;
		    String type="application/x-www-form-urlencoded";
		    conn=(HttpURLConnection)url.openConnection();
		    conn.setDoInput(true);
		    conn.setDoOutput(true);
		    conn.setRequestMethod("POST");
		    conn.setRequestProperty( "User-Agent", agent );
		    conn.setRequestProperty( "Content-Type", type );
		    conn.setRequestProperty( "Content-Length", ""+query.length());

		    OutputStream out=conn.getOutputStream();
		    out.write(query.getBytes());
		    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    in.close();
		    int rc = conn.getResponseCode();
		    System.out.print("Response Code = "+rc+"\n");
		    String rm=conn.getResponseMessage();
		    System.out.print("Response Message = "+rm+"\n");
		    }catch(Exception e){
		    e.printStackTrace();
		    }finally{
		    conn.disconnect();
		    }
		    }
	    
	    public void db_loeschen(String delete){
		    HttpURLConnection conn=null;
		    try{
		    URL url=new URL("http://joergy.funpic.de/db_zugriff.php");
		    String agent="Applet";
		    String query="query=" + delete;
		    String type="application/x-www-form-urlencoded";
		    conn=(HttpURLConnection)url.openConnection();
		    conn.setDoInput(true);
		    conn.setDoOutput(true);
		    conn.setRequestMethod("POST");
		    conn.setRequestProperty( "User-Agent", agent );
		    conn.setRequestProperty( "Content-Type", type );
		    conn.setRequestProperty( "Content-Length", ""+query.length());

		    OutputStream out=conn.getOutputStream();
		    out.write(query.getBytes());
		    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		    in.close();
		    int rc = conn.getResponseCode();
		    System.out.print("Response Code = "+rc+"\n");
		    String rm=conn.getResponseMessage();
		    System.out.print("Response Message = "+rm+"\n");
		    }catch(Exception e){
		    e.printStackTrace();
		    }finally{
		    conn.disconnect();
		    }
		    }
	    
	    public int getanzahl(){
	    	return anzahl;  
	    	} 
	    
	    public String[] getip_adressen(){
	    	return ip_adressen;  
	    	}  
	}
